// CyTargetLinker,
// a Cytoscape plugin to extend biological networks with regulatory interaction
// Copyright 2011-2012 BiGCaT Bioinformatics
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package cytargetlinker;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Collections;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.pdf.forms.gui.properties.customcomponents.colorcombobox.ColorComboBoxEditor;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import com.l2fprod.common.swing.renderer.ColorCellRenderer;

import cytoscape.Cytoscape;
import cytoscape.view.cytopanels.CytoPanelImp;

/**
 * tab in the result panel
 * shows the number of added interactions
 * for each RIN
 * possibility to change the edge colors
 */
public class ResultTab {
	private CyTargetLinker linker;
	private CellConstraints cc = new CellConstraints();
	private JComboBox colorComboBox;

	private Component comp;
	private JSpinner field;
	
	//show the databaseColor panel
	public ResultTab(CyTargetLinker linker) {
		this.linker = linker;
	}
	
	public void fillTab() {
		// Visualize results in ResultsPanel (EAST)
		CytoPanelImp ctrlPanel = (CytoPanelImp)Cytoscape.getDesktop().getCytoPanel(SwingConstants.EAST);

		// Add it to the control panel.
		comp = ctrlPanel.add(" " + linker.getResultingNetworkName() + " ", getDatabasePanel());
		comp.setVisible(true);
	}
	
	// Create a database and color panel
	private JPanel getDatabasePanel() {
		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		panel.setLayout(new BorderLayout());
		panel.add(getDataColorPanel(), BorderLayout.CENTER);
		
		return panel;
	}

	private Component getDataColorPanel() {
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		panel.setBackground(Color.white);
		
		panel.add(fillPanel(), BorderLayout.NORTH);
		
		JScrollPane pane = new JScrollPane(panel);
		pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		
		return pane;
	}

	private JPanel fillPanel() {
		String rowLayout = "p,15dlu";
		for(int i = 0; i < linker.getRegulatoryInteractionNetworks().size(); i++) {
			rowLayout = rowLayout + ",p,5dlu";
		}
		rowLayout = rowLayout + ", 15dlu, p, 5dlu";
		FormLayout layout = new FormLayout("pref,10dlu, pref, 10dlu, 30dlu, 10dlu, pref", rowLayout);
		PanelBuilder builder = new PanelBuilder(layout);
		builder.setDefaultDialogBorder();
		builder.setBackground(Color.WHITE);
		
		//create a column with the name of the database found in the file selected
		builder.addLabel("RIN", cc.xy(1, 1));
		builder.addLabel("#", cc.xy(3,1));
		builder.addLabel("Color", cc.xy(5, 1));	
		builder.addSeparator("", cc.xyw(1,2,5));
		
		int i = 0;
		Collections.sort(linker.getRegulatoryInteractionNetworks());
		
		for(final RegulatoryInteractionNetwork net : linker.getRegulatoryInteractionNetworks()) {
			builder.addLabel(net.getName(), cc.xy(1, ((i+1)*2)+1));
			builder.addLabel(""+ net.getNewInteractions(), cc.xy(3, ((i+1)*2)+1));
			colorComboBox = new JComboBox(Preferences.getInstance().getColors().toArray());
			colorComboBox.setEditable(true);
			colorComboBox.setRenderer(new ColorCellRenderer());
		    //show the initial color in the first case of the JComboBox
			colorComboBox.setEditor(new ColorComboBoxEditor(net.getColor()));
			// change the initial color
			colorComboBox.setSelectedItem(net.getColor());

			colorComboBox.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent itemEvent) {
					
						if(itemEvent.getStateChange() == ItemEvent.SELECTED) {
							JComboBox source = (JComboBox)itemEvent.getSource();
							int color = source.getSelectedIndex();

							net.setColor(Preferences.getInstance().getColors().get(color));
							CyTargetLinkerVisualStyle style = new CyTargetLinkerVisualStyle();	
							Cytoscape.getNetworkView(linker.getResultingNetwork().getIdentifier()).applyVizmapper(style.createVisualStyle(linker));
							Cytoscape.createNetworkView(linker.getResultingNetwork(), "CyTargetLinkerView");
						}
					}
			});
			builder.add(colorComboBox, cc.xy(5, ((i + 1) * 2) + 1));
			
			String[] show = { "Show", "Hide" };
			JComboBox box = new JComboBox(show);
			box.setSelectedIndex(0);
			box.addItemListener(new ItemListener() {
				
				@Override
				public void itemStateChanged(ItemEvent itemEvent) {
					JComboBox source = (JComboBox)itemEvent.getSource();
					if(source.getSelectedItem().equals("Show")) {
						net.setHidden(false);
						RINHandler.getHandler().showRIN(net, linker);
						RINHandler.getHandler().applyThreshold(linker);
						linker.applyVisualStyle();
					} else {
						net.setHidden(true);
						RINHandler.getHandler().hideRIN(net, linker);
						RINHandler.getHandler().applyThreshold(linker);
						linker.applyVisualStyle();
					}
				}		
				
			});
			
			builder.add(box, cc.xy(7, ((i+1)*2)+1));
			i++;	
		}

		// (initial value, min, max, step size)
		SpinnerModel model = new SpinnerNumberModel(1, 1, linker.getRegulatoryInteractionNetworks().size()+1, 1);
		field = new JSpinner(model);
		field.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent arg0) {
				Integer i = (Integer) field.getValue();
				linker.setTreshhold(i);
				RINHandler.getHandler().applyThreshold(linker);
				linker.applyVisualStyle();
				linker.applyLayout();
			}
		});
		
		builder.add(new JLabel("Supported by"), cc.xy(1, 4 + (linker.getRegulatoryInteractionNetworks().size()*2)));
		builder.add(field, cc.xy(3, 4 + (linker.getRegulatoryInteractionNetworks().size()*2)));
		builder.add(new JLabel("RINs"), cc.xy(5, 4 + (linker.getRegulatoryInteractionNetworks().size()*2)));
		
		return  builder.getPanel();
	}

	public void removeTab() {
		CytoPanelImp ctrlPanel = (CytoPanelImp) Cytoscape.getDesktop().getCytoPanel(SwingConstants.EAST);
		ctrlPanel.remove(comp);
	}	
	
}
